/**  
 * @Title:  CorsFilter.java   
 * @Package com.platform.config    
 * @author: <a href="mailto:15909910367@163.com">吴先生</a> 
 * @date:   2022年11月2日 下午6:25:57   
 * @version V1.0 
 * @Copyright: 2022 吴先生.
 */
package com.platform.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**   
 * @ClassName:  CorsFilter   
 * @Description: 跨域配置
 * @date:   2022年11月2日 下午6:25:57   
 * @author: <a href="mailto:15909910367@163.com">吴先生</a>  
 * @Copyright: 2022 吴元森.  
 */
@Configuration
@ConditionalOnExpression("${platform.cors.allow}")
public class CorsConfig {

	@Value("${platform.cors.mapping}")
	private String mapping; //限定的跨域资源
	@Value("${platform.cors.origin}")
	private String origin;	//允许的跨域请求方式，* 需要以单引号'
	@Value("${platform.cors.method}")
	private String method;	//允许的请求域
	
	@Bean
	public CorsFilter corsFilter() {
		CorsConfiguration config = new CorsConfiguration();
		config.addAllowedOrigin(origin);
		config.setAllowCredentials(Boolean.TRUE);
		config.addAllowedMethod(method);
		config.addAllowedHeader(origin);
		UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource();
		configSource.registerCorsConfiguration(mapping, config);
		return new CorsFilter(configSource);
	}
}
